/*! ******************************************************************************
 *
 * Pentaho
 *
 * Copyright (C) 2024 by Hitachi Vantara, LLC : http://www.pentaho.com
 *
 * Use of this software is governed by the Business Source License included
 * in the LICENSE.TXT file.
 *
 * Change Date: 2029-07-20
 ******************************************************************************/


package org.pentaho.di.trans.steps.combinationlookup;

import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.pentaho.di.core.database.Database;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.row.RowMetaInterface;
import org.pentaho.di.junit.rules.RestorePDIEnvironment;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.trans.step.StepMeta;
import org.pentaho.di.trans.step.StepPartitioningMeta;

import java.sql.Connection;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

public class CombinationLookupTest {
  @ClassRule public static RestorePDIEnvironment env = new RestorePDIEnvironment();
  private DatabaseMeta databaseMeta;

  private StepMeta stepMeta;

  private CombinationLookup combinationLookup;
  private CombinationLookupMeta combinationLookupMeta;
  private CombinationLookupData combinationLookupData;

  @Before
  public void setup() {
    databaseMeta = mock( DatabaseMeta.class );
    doReturn( "" ).when( databaseMeta ).quoteField( anyString() );

    combinationLookupMeta = mock( CombinationLookupMeta.class );
    doReturn( databaseMeta ).when( combinationLookupMeta ).getDatabaseMeta();
    doReturn( "sasas" ).when( combinationLookupMeta ).getTechnicalKeyField();
    doReturn( new String[] {} ).when( combinationLookupMeta ).getKeyLookup();

    stepMeta = mock( StepMeta.class );
    doReturn( "step" ).when( stepMeta ).getName();
    doReturn( mock( StepPartitioningMeta.class ) ).when( stepMeta ).getTargetStepPartitioningMeta();
    doReturn( combinationLookupMeta ).when( stepMeta ).getStepMetaInterface();

    Database db = mock( Database.class );
    doReturn( mock( Connection.class ) ).when( db ).getConnection();

    combinationLookupData = mock( CombinationLookupData.class );
    combinationLookupData.db = db;
    combinationLookupData.keynrs = new int[] {};

    TransMeta transMeta = mock( TransMeta.class );
    doReturn( stepMeta ).when( transMeta ).findStep( anyString() );

    Trans trans = mock( Trans.class );
    combinationLookup = spy( new CombinationLookup( stepMeta, combinationLookupData, 1, transMeta, trans ) );
    doReturn( false ).when( combinationLookup ).isRowLevel();
    doReturn( true ).when( combinationLookup ).isAutoIncrement();
    doNothing().when( combinationLookup ).logDetailed( anyString() );
  }

  @Test
  public void testCombiInsert() throws Exception {
    combinationLookup.combiInsert( any( RowMetaInterface.class ), any( Object[].class ), anyLong(), anyLong() );
    verify( databaseMeta, times( 2 ) ).supportsAutoGeneratedKeys();
  }
}
